package com.yami.shop.api.controller;

import com.yami.shop.api.request.NoticeListRequest;
import com.yami.shop.bean.app.dto.NoticeMallDto;
import com.yami.shop.bean.model.NoticeModel;
import com.yami.shop.service.NoticeMallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/mall/notice")
@Api(tags = "商城通知接口")
@AllArgsConstructor
public class NoticeMallController {

    @Autowired
    private NoticeMallService noticeMallService;

    @Autowired
    private MapperFacade mapperFacade;

    /**
     * 通知列表接口
     */
    @GetMapping("/noticeList")
    @ApiOperation(value = "通知列表接口", notes = "获取所有通知列表信息")
    public ResponseEntity<List<NoticeMallDto>> getNoticeList() {
        List<NoticeModel> noticeList = noticeMallService.getlistByNoticeFlag();
        if(CollectionUtils.isEmpty(noticeList)){
            return ResponseEntity.ok(new ArrayList());
        }
        List<NoticeMallDto> noticeDtoList = mapperFacade.mapAsList(noticeList, NoticeMallDto.class);
        noticeDtoList.forEach(p->{
            p.setNoticeTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(p.getCreatedStamp()));
        });
        return ResponseEntity.ok(noticeDtoList);
    }

    @PostMapping("/doNotice")
    @ApiOperation(value = "通知列表接口", notes = "获取所有通知列表信息")
    public ResponseEntity<Boolean> doNotice(@RequestBody NoticeListRequest request) {
        noticeMallService.updateFlagByIds(request.getList());
        return ResponseEntity.ok(Boolean.TRUE);
    }

}
